package cn.zh.stan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.renderscript.Sampler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.CAItem;
import cn.zh.stan.ITem.GooodItem;
import cn.zh.stan.ITem.Order;
import cn.zh.stan.ITem.ReItem;
import cn.zh.stan.ITem.SItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.adapter.FAdapter;
import cn.zh.stan.util.MyListView;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class OrderDetail extends BaseActivity {
    Order order=null;
    ArrayList<GooodItem>gooodItems;
    ArrayList<CAItem> datas=new ArrayList<CAItem>();

    ImageView fil_back;
    LinearLayout readress,sendformat;
    TextView totaey;
    double tomon=0;
    TextView sytxt;
    MyListView flist;
    FAdapter fAdapter;
    TextView suborder;
    TextView fname,fphon,fadres,otmoney,ordernum,creatime,paytime;

    TextView pttxt,sentime,adone,adtwo;

    Dialog dialog;

    String uid;
    MyApplication mapp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        order= (Order) getIntent().getSerializableExtra("cd");
        gooodItems=order.getArrayList();
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        datas.clear();
        for (int i=0;i<gooodItems.size();i++){
            GooodItem gooodItem=gooodItems.get(i);
            CAItem caItem=new CAItem(gooodItem.getId(),gooodItem.getGoodsId(),gooodItem.getSellCount(),gooodItem.getTradeNo(),gooodItem.getGoodsNo(),gooodItem.getUnit(),Double.valueOf(gooodItem.getSellPrice()),true);
            datas.add(caItem);
           }
        initod();
          }



      public void initod(){
          fil_back= (ImageView) findViewById(R.id.fil_back);
          readress= (LinearLayout) findViewById(R.id.readress);
          sendformat= (LinearLayout) findViewById(R.id.sendformat);
          totaey=(TextView)findViewById(R.id.totaey);
          sytxt=(TextView)findViewById(R.id.sytxt);

          suborder=(TextView)findViewById(R.id.suborder);
          fname=(TextView)findViewById(R.id.fname);
          fphon=(TextView)findViewById(R.id.fphon);
          fadres=(TextView)findViewById(R.id.fadres);
          otmoney=(TextView)findViewById(R.id.otmoney);
          ordernum=(TextView)findViewById(R.id.ordernum);
          creatime=(TextView)findViewById(R.id.creatime);
          paytime=(TextView)findViewById(R.id.paytime);
          pttxt=(TextView)findViewById(R.id.pttxt);
          sentime=(TextView)findViewById(R.id.sentime);
          adone=(TextView)findViewById(R.id.adone);
          adtwo=(TextView)findViewById(R.id.adtwo);

          for (int j=0;j<datas.size();j++){
              tomon=tomon+datas.get(j).getNum()*datas.get(j).getPrice_Detail();
          }
          totaey.setText(tomon+"");
          //订单总价
          otmoney.setText(tomon+"");

          ordernum.setText(order.getTradeNo());
          creatime.setText(order.getCreateTime());
          paytime.setText(order.getOrdePayTime());
          pttxt.setText(order.getOrdePayType());
          sentime.setText(order.getSendTime());

          flist= (MyListView) findViewById(R.id.flist);
          fAdapter=new FAdapter(OrderDetail.this,datas);
          flist.setAdapter(fAdapter);
          fil_back.setOnClickListener(this);
          sytxt.setText(order.getLogisticName());
          fname.setText("收货人:"+order.getPersonName());
          fphon.setText(order.getMoblie());
          fadres.setText("收货地址:"+order.getProvinceName()+order.getCityName()+order.getCountyName()+order.getAddress()+order.getCustomerName());

          switch (order.getTradeStatus()){
              case "0":
                  adone.setVisibility(View.VISIBLE);
                  adone.setText("关闭订单");
                  adtwo.setText("支付");
                  break;
              case "1":
                  adone.setVisibility(View.GONE);
                  adtwo.setText("再次购买");
                  break;
              case "2":
                  adone.setVisibility(View.VISIBLE);
                  adone.setText("再次购买");
                  adtwo.setText("确认收货");
                  break;
              case "99":
                  adone.setVisibility(View.GONE);
                  adtwo.setText("再次购买");
                  break;
               }

          adone.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                 switch (order.getTradeStatus()){
                     case "0":
                         //关闭订单
                         getDialog();
                         break;
                     case "2":
                         //再次购买
                         Intent intent3=new Intent(OrderDetail.this, CarActivity.class);
                         startActivity(intent3);
                         break;
                      }
                    }
                 });


          adtwo.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  switch (order.getTradeStatus()){
                      case "0":
                          //支付
                          Intent intent=new Intent(OrderDetail.this, ToPay.class);
                          intent.putExtra("tradeNo",order.getTradeNo());
                          intent.putExtra("tomon",order.getGoodsTotal());
                          startActivity(intent);
                          break;
                      case "1":
                          //再次购买
                          Intent intent1=new Intent(OrderDetail.this, CarActivity.class);
                          startActivity(intent1);
                          break;
                      case "2":
                          //确认收货
                          getsure();
                          break;
                      case "99":
                          //再次购买
                          Intent intent2=new Intent(OrderDetail.this, CarActivity.class);
                          startActivity(intent2);
                          break;
                     }
                  }
              });
           }



    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.fil_back:
                finish();
                break;
            case R.id.suborder:
                //提交订单
                Intent intent1=new Intent(OrderDetail.this,ToPay.class);
                intent1.putExtra("tradeNo",order.getTradeNo());
                String str=tomon+"";
                intent1.putExtra("tomon",str);
                startActivity(intent1);
                break;
               }
             }




    public void getDialog(){
        LayoutInflater inflater = LayoutInflater.from(OrderDetail.this);
        View v = inflater.inflate(R.layout.clodialog, null);// 得到加载view
        LinearLayout layou = (LinearLayout) v.findViewById(R.id.clo);// 加载布局
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否要关闭订单");
        dialog = new Dialog(OrderDetail.this, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layou,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确定关闭订单
                dialog.dismiss();
                closeorder(order.getId());
            }
        });
      }




    public void getsure(){
        LayoutInflater inflater = LayoutInflater.from(OrderDetail.this);
        View v = inflater.inflate(R.layout.clodialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.clo);// 加载布局

        dialog = new Dialog(OrderDetail.this, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否确认收货");
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确定收货
                dialog.dismiss();
                getReceive();
            }
        });
    }




    public  void closeorder(String id){
        OkHttpUtils.post(BaseUrl.CloseOrder)
                .params("userId",uid)
                .params("id",id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("----------->关闭订单成功==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(OrderDetail.this,Msg);
                                if (Flag){
                                 finish();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
              }


    public void getReceive(){
        OkHttpUtils.post(BaseUrl.Receive)
                .params("tradeNo",order.getTradeNo())
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        JSONObject js= null;
                        try {
                            js = new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(OrderDetail.this,Msg);
                                if (Flag){
                                 finish();
                                }
                             }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
        }













}
